using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class WizardController : MonoBehaviour
{
    private Rigidbody _rb;
    private Animator _animator;
    [SerializeField]private float _rotateSpeed;
    [Range(2, 5)] public float _moveSpeed;
    

    private Vector2 _moveInput;
    private Vector3 _direction;

    private void Start()
    {
        _animator = GetComponent<Animator>();
        _rb=GetComponent<Rigidbody>();
    }

    public void GetMoveInput(InputAction.CallbackContext ctx)
    {
        _moveInput=ctx.ReadValue<Vector2>();
    }
    public void Move(Vector3 direction) 
    {

        var dir = direction.normalized;
      
        Vector3 targetPosition=_rb.position + dir * _moveSpeed * Time.deltaTime;

        Vector3 targetVelocity = (targetPosition - transform.position) / Time.deltaTime;
        _rb.velocity = new Vector3(targetVelocity.x, _rb.velocity.y, targetVelocity.z);

        _animator.SetFloat("HorizontalSpeed", dir.x * _moveSpeed, .1f, Time.deltaTime);
        _animator.SetFloat("VerticalSpeed", dir.z * _moveSpeed, .1f, Time.deltaTime);
    }

    public void Rotate(Vector3 direction) 
    {
        direction.y = 0;
        Vector3 targetRotate=Vector3.RotateTowards(transform.forward, direction.normalized, _rotateSpeed *Time.deltaTime,.1f);
        Quaternion rad=Quaternion.LookRotation(targetRotate);
        transform.rotation = rad;
    }

    private void FixedUpdate()
    {
        _direction=new Vector3(_moveInput.x,0,_moveInput.y);
        Rotate(_direction);
        Move(_direction);
    }

}

